#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
struct Stu
{
    string id, name;
    int grade;
};
bool cmp1(Stu s1, Stu s2)
{
    return s1.id < s2.id;
}
bool cmp2(Stu s1, Stu s2)
{
    return s1.name != s2.name ? s1.name < s2.name : s1.id < s2.id;
}
bool cmp3(Stu s1, Stu s2)
{
    return s1.grade != s2.grade ? s1.grade < s2.grade : s1.id < s2.id;
}
int main()
{
    // freopen("case.txt", "r", stdin);
    int n, c;
    Stu s;
    vector<Stu> sheet;
    cin >> n >> c;
    for (int i = 0; i < n; i++)
    {
        cin >> s.id >> s.name >> s.grade;
        sheet.push_back(s);
    }
    switch (c)
    {
    case 1:
        sort(sheet.begin(), sheet.end(), cmp1);
        break;
    case 2:
        sort(sheet.begin(), sheet.end(), cmp2);
        break;
    case 3:
        sort(sheet.begin(), sheet.end(), cmp3);
        break;
    }
    for (int i = 0; i < sheet.size(); i++)
    {
        cout << sheet[i].id << " " << sheet[i].name << " " << sheet[i].grade << endl;
    }
    return 0;
}